package com.hsq.androidproject.ui;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.Toast;

import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.adapter.QXzpAdapter;
import com.hsq.androidproject.entity.QXzp;
import com.hsq.androidproject.utils.OkHttpClientManager;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class QXzpFragment extends Fragment {//作品集  音乐MV
    private GridView mZxGridView;
    String path = "";

    public QXzpFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_qxzp, container, false);
        initView(view);
        Bundle bundle = getArguments();    /////ShowHotStartsActivity
        final int sid = bundle.getInt("sid", 0);
        addData(sid);
        return view;
    }

    private void addData(int sid) {
          /*音乐MV*/
        path = "http://api.hanju.koudaibaobao.com/api/star/works?sid=" + sid + "&cate=2&offset=0&count=80";
        OkHttpClientManager.getAsyn(path, new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                Toast.makeText(getContext(), "出错啦！", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {
                QXzp qXzp = new Gson().fromJson(response, QXzp.class);
                List<QXzp.WorksBean> worksBeen = qXzp.getWorks();
                QXzpAdapter adapter = new QXzpAdapter(worksBeen, getContext());
                mZxGridView.setAdapter(adapter);
            }
        });
    }

    private void initView(View view) {
        mZxGridView = (GridView) view.findViewById(R.id.mZxGridView);
    }
}